
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>循环v-for的首次渲染和重新渲染 · petite-vue源码剖析</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        <meta name="author" content="^_^肥仔John">
        
        
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search-pro/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-code/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-back-to-top-button/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-tbfed-pagefooter/footer.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-chapter-fold/chapter-fold.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-expandable-chapters/expandable-chapters.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-splitter/splitter.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-anchor-navigation-ex/style/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-donate/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-theme-comscore/test.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="4_directives/1_attribute_binding.html" />
    
    
    <link rel="prev" href="2_initial_rendering_and_rerendering_for_v-if.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        <li class="header">前言</li>
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    《Petite-Vue源码剖析》简介
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../0_intro/1_reason_for_source_reading.html">
            
                <a href="../0_intro/1_reason_for_source_reading.html">
            
                    
                    为什么要读源码
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">在线渲染</li>
        
        
    
        <li class="chapter " data-level="2.1" data-path="0_intro.html">
            
                <a href="0_intro.html">
            
                    
                    章节介绍
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="1_start_from_static_view.html">
            
                <a href="1_start_from_static_view.html">
            
                    
                    从静态视图首次渲染开始
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="2_initial_rendering_and_rerendering_for_v-if.html">
            
                <a href="2_initial_rendering_and_rerendering_for_v-if.html">
            
                    
                    条件v-if的首次渲染和重新渲染
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="2.4" data-path="3_initial_rendering_and_rerendering_for_v-for.html">
            
                <a href="3_initial_rendering_and_rerendering_for_v-for.html">
            
                    
                    循环v-for的首次渲染和重新渲染
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.5" data-path="4_directives/1_attribute_binding.html">
            
                <a href="4_directives/1_attribute_binding.html">
            
                    
                    属性绑定v-bind的工作原理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.6" data-path="4_directives/2_event_binding.html">
            
                <a href="4_directives/2_event_binding.html">
            
                    
                    事件绑定v-on的工作原理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.7" data-path="4_directives/3_bidirectional_binding.html">
            
                <a href="4_directives/3_bidirectional_binding.html">
            
                    
                    双向绑定v-model的工作原理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.8" data-path="4_directives/4_ref.html">
            
                <a href="4_directives/4_ref.html">
            
                    
                    特殊指令ref的工作原理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.9" data-path="5_tips_for_template.html">
            
                <a href="5_tips_for_template.html">
            
                    
                    优化手段template的原理和使用技巧
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">响应式系统</li>
        
        
    
        <li class="chapter " data-level="3.1" data-path="../2_reactivity_system/0_intro.html">
            
                <a href="../2_reactivity_system/0_intro.html">
            
                    
                    章节介绍
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="../2_reactivity_system/1_reactive_object_array.html">
            
                <a href="../2_reactivity_system/1_reactive_object_array.html">
            
                    
                    逐行解读reactive如何构建对象和数组的响应式对象
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="../2_reactivity_system/2_reactive_map_set.html">
            
                <a href="../2_reactivity_system/2_reactive_map_set.html">
            
                    
                    逐行解读reactive如何构建Map和Set的响应式对象
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.4" data-path="../2_reactivity_system/3_register_side_effect.html">
            
                <a href="../2_reactivity_system/3_register_side_effect.html">
            
                    
                    逐行解读effect如何注册副作用函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.5" data-path="../2_reactivity_system/4_cleanup_algorithm.html">
            
                <a href="../2_reactivity_system/4_cleanup_algorithm.html">
            
                    
                    依赖清理优化算法详解-算法思路
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.6" data-path="../2_reactivity_system/5_smi.html">
            
                <a href="../2_reactivity_system/5_smi.html">
            
                    
                    依赖清理优化算法详解-基于JavaScript引擎的SMI进一步压榨性能
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.7" data-path="../2_reactivity_system/6_trigger_and_scheduler.html">
            
                <a href="../2_reactivity_system/6_trigger_and_scheduler.html">
            
                    
                    逐行解读trigger和调度器
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">沙箱模型</li>
        
        
    
        <li class="chapter " data-level="4.1" data-path="../3_sandbox.html">
            
                <a href="../3_sandbox.html">
            
                    
                    模板表达式的沙箱模型
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >循环v-for的首次渲染和重新渲染</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <div id="anchor-navigation-ex-navbar"><i class="fa fa-navicon"></i><ul><li><span class="title-icon "></span><a href="#&#x5FAA;&#x73AF;v-for&#x7684;&#x9996;&#x6B21;&#x6E32;&#x67D3;&#x548C;&#x66F4;&#x65B0;&#x6E32;&#x67D3;"><b>1. </b>&#x5FAA;&#x73AF;v-for&#x7684;&#x9996;&#x6B21;&#x6E32;&#x67D3;&#x548C;&#x66F4;&#x65B0;&#x6E32;&#x67D3;</a></li><ul><li><span class="title-icon "></span><a href="#v-for&#x7684;&#x9996;&#x6B21;&#x6E32;&#x67D3;"><b>1.1. </b>v-for&#x7684;&#x9996;&#x6B21;&#x6E32;&#x67D3;</a></li><ul><li><span class="title-icon "></span><a href="#&#x89E3;&#x6790;option-v-forval-in-values-v-keyvalim-the-one-of-optionsoption"><b>1.1.1. </b>&#x89E3;&#x6790;<option v-for="val in values" v-key="val">I&apos;m the one of options</option></a></li></ul><li><span class="title-icon "></span><a href="#v-for&#x7684;&#x66F4;&#x65B0;&#x6E32;&#x67D3;"><b>1.2. </b>v-for&#x7684;&#x66F4;&#x65B0;&#x6E32;&#x67D3;</a></li><ul><li><span class="title-icon "></span><a href="#key&#x5143;&#x7D20;&#x590D;&#x7528;&#x7B97;&#x6CD5;&#x8BE6;&#x89E3;"><b>1.2.1. </b>key&#x5143;&#x7D20;&#x590D;&#x7528;&#x7B97;&#x6CD5;&#x8BE6;&#x89E3;</a></li><li><span class="title-icon "></span><a href="#&#x548C;react&#x901A;&#x8FC7;key&#x590D;&#x7528;&#x5143;&#x7D20;&#x7684;&#x533A;&#x522B;&#xFF1F;"><b>1.2.2. </b>&#x548C;React&#x901A;&#x8FC7;key&#x590D;&#x7528;&#x5143;&#x7D20;&#x7684;&#x533A;&#x522B;&#xFF1F;</a></li></ul><li><span class="title-icon "></span><a href="#&#x5C0F;&#x7ED3;"><b>1.3. </b>&#x5C0F;&#x7ED3;</a></li></ul></ul></div><h1 id="&#x5FAA;&#x73AF;v-for&#x7684;&#x9996;&#x6B21;&#x6E32;&#x67D3;&#x548C;&#x66F4;&#x65B0;&#x6E32;&#x67D3;"><a name="&#x5FAA;&#x73AF;v-for&#x7684;&#x9996;&#x6B21;&#x6E32;&#x67D3;&#x548C;&#x66F4;&#x65B0;&#x6E32;&#x67D3;" class="anchor-navigation-ex-anchor" href="#&#x5FAA;&#x73AF;v-for&#x7684;&#x9996;&#x6B21;&#x6E32;&#x67D3;&#x548C;&#x66F4;&#x65B0;&#x6E32;&#x67D3;"><i class="fa fa-link" aria-hidden="true"></i></a>1. &#x5FAA;&#x73AF;v-for&#x7684;&#x9996;&#x6B21;&#x6E32;&#x67D3;&#x548C;&#x66F4;&#x65B0;&#x6E32;&#x67D3;</h1>
<h2 id="v-for&#x7684;&#x9996;&#x6B21;&#x6E32;&#x67D3;"><a name="v-for&#x7684;&#x9996;&#x6B21;&#x6E32;&#x67D3;" class="anchor-navigation-ex-anchor" href="#v-for&#x7684;&#x9996;&#x6B21;&#x6E32;&#x67D3;"><i class="fa fa-link" aria-hidden="true"></i></a>1.1. v-for&#x7684;&#x9996;&#x6B21;&#x6E32;&#x67D3;</h2>
<p>&#x4E0B;&#x9762;&#x6211;&#x4EEC;&#x5728;&#x9759;&#x6001;&#x89C6;&#x56FE;&#x7684;&#x57FA;&#x7840;&#x4E0A;&#x52A0;&#x5165;<code>v-for</code>&#x6307;&#x4EE4;&#xFF0C;&#x5E76;&#x901A;&#x8FC7;&#x4EBA;&#x8089;&#x5355;&#x6B65;&#x8C03;&#x8BD5;&#x7684;&#x65B9;&#x5F0F;&#x770B;&#x770B;<code>v-for</code>&#x5230;&#x5E95;&#x662F;&#x5982;&#x4F55;&#x6E32;&#x67D3;&#x7684;&#x5427;&#xFF01;</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">v-scope</span>=<span class="hljs-string">&quot;App&quot;</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>

<span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;module&quot;</span>&gt;</span><span class="handlebars"><span class="xml">
  import { createApp } from &apos;https://unpkg.com/petite-vue?module&apos;

  createApp({
    App: {
      $template: `
      <span class="hljs-tag">&lt;<span class="hljs-name">select</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">option</span> <span class="hljs-attr">v-for</span>=<span class="hljs-string">&quot;val of values&quot;</span> <span class="hljs-attr">v-key</span>=<span class="hljs-string">&quot;val&quot;</span>&gt;</span>
          I&apos;m the one of options
        <span class="hljs-tag">&lt;/<span class="hljs-name">option</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">select</span>&gt;</span>
      `,
    }
    values: [1,2,3]
  }).mount(&apos;[v-scope]&apos;)
</span></span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
</code></pre>
<p>&#x4EBA;&#x8089;&#x5355;&#x6B65;&#x8C03;&#x8BD5;&#xFF1A;</p>
<ol>
<li>&#x8C03;&#x7528;<code>createApp</code>&#x6839;&#x636E;&#x5165;&#x53C2;&#x751F;&#x6210;&#x5168;&#x5C40;&#x4F5C;&#x7528;&#x57DF;<code>rootScope</code>&#xFF0C;&#x521B;&#x5EFA;&#x6839;&#x4E0A;&#x4E0B;&#x6587;<code>rootCtx</code>&#xFF1B;</li>
<li>&#x8C03;&#x7528;<code>mount</code>&#x4E3A;<code>&lt;div v-scope=&quot;App&quot;&gt;&lt;/div&gt;</code>&#x6784;&#x5EFA;&#x6839;&#x5757;&#x5BF9;&#x8C61;<code>rootBlock</code>&#xFF0C;&#x5E76;&#x5C06;&#x5176;&#x4F5C;&#x4E3A;&#x6A21;&#x677F;&#x6267;&#x884C;&#x89E3;&#x6790;&#x5904;&#x7406;&#xFF1B;</li>
<li>&#x89E3;&#x6790;&#x65F6;&#x8BC6;&#x522B;&#x5230;<code>v-scope</code>&#x5C5E;&#x6027;&#xFF0C;&#x4EE5;&#x5168;&#x5C40;&#x4F5C;&#x7528;&#x57DF;<code>rootScope</code>&#x4E3A;&#x57FA;&#x7840;&#x8FD0;&#x7B97;&#x5F97;&#x5230;&#x5C40;&#x90E8;&#x4F5C;&#x7528;&#x57DF;<code>scope</code>&#xFF0C;&#x5E76;&#x4EE5;&#x6839;&#x4E0A;&#x4E0B;&#x6587;<code>rootCtx</code>&#x4E3A;&#x84DD;&#x672C;&#x4E00;&#x540C;&#x6784;&#x5EFA;&#x65B0;&#x7684;&#x4E0A;&#x4E0B;&#x6587;<code>ctx</code>&#xFF0C;&#x7528;&#x4E8E;&#x5B50;&#x8282;&#x70B9;&#x7684;&#x89E3;&#x6790;&#x548C;&#x6E32;&#x67D3;&#xFF1B;</li>
<li>&#x83B7;&#x53D6;<code>$template</code>&#x5C5E;&#x6027;&#x503C;&#x5E76;&#x751F;&#x6210;HTML&#x5143;&#x7D20;&#xFF1B;</li>
<li>&#x6DF1;&#x5EA6;&#x4F18;&#x5148;&#x904D;&#x5386;&#x89E3;&#x6790;&#x5B50;&#x8282;&#x70B9;(&#x8C03;&#x7528;<code>walkChildren</code>)&#xFF1B;</li>
<li>&#x89E3;&#x6790;<code>&lt;option v-for=&quot;val in values&quot; v-key=&quot;val&quot;&gt;I&apos;m the one of options&lt;/option&gt;</code></li>
</ol>
<h3 id="&#x89E3;&#x6790;option-v-forval-in-values-v-keyvalim-the-one-of-optionsoption"><a name="&#x89E3;&#x6790;option-v-forval-in-values-v-keyvalim-the-one-of-optionsoption" class="anchor-navigation-ex-anchor" href="#&#x89E3;&#x6790;option-v-forval-in-values-v-keyvalim-the-one-of-optionsoption"><i class="fa fa-link" aria-hidden="true"></i></a>1.1.1. &#x89E3;&#x6790;&lt;option v-for=&quot;val in values&quot; v-key=&quot;val&quot;&gt;I&apos;m the one of options&lt;/option&gt;</h3>
<p>&#x5F53;&#x9047;&#x5230;<code>v-for</code>&#x6307;&#x4EE4;&#x65F6;&#x4EE3;&#x7801;&#x4F1A;&#x6309;&#x5982;&#x4E0B;&#x6D41;&#x7A0B;&#x6267;&#x884C;&#xFF1A;</p>
<ol>
<li>&#x8BC6;&#x522B;&#x5143;&#x7D20;&#x5E26;&#x4E0A;<code>v-for</code>&#x5C5E;&#x6027;&#xFF0C;&#x8C03;&#x7528;<code>_for</code>&#x5143;&#x6307;&#x4EE4;&#x5BF9;&#x8BE5;&#x5143;&#x7D20;&#x89E3;&#x6790;&#xFF1B;</li>
<li>&#x901A;&#x8FC7;&#x6B63;&#x5219;&#x8868;&#x8FBE;&#x5F0F;&#x63D0;&#x53D6;<code>v-for</code>&#x4E2D;&#x96C6;&#x5408;&#x548C;&#x96C6;&#x5408;&#x5143;&#x7D20;&#x7684;&#x8868;&#x8FBE;&#x5F0F;&#x5B57;&#x7B26;&#x4E32;&#xFF0C;&#x548C;<code>key</code>&#x7684;&#x8868;&#x8FBE;&#x5F0F;&#x5B57;&#x7B26;&#x4E32;&#xFF1B;</li>
<li>&#x57FA;&#x4E8E;&#x6BCF;&#x4E2A;&#x96C6;&#x5408;&#x5143;&#x7D20;&#x521B;&#x5EFA;&#x72EC;&#x7ACB;&#x4F5C;&#x7528;&#x57DF;&#xFF0C;&#x5E76;&#x521B;&#x5EFA;&#x72EC;&#x7ACB;&#x7684;&#x5757;&#x5BF9;&#x8C61;&#x6E32;&#x67D3;&#x5143;&#x7D20;&#x3002;</li>
</ol>
<pre><code class="lang-ts"><span class="hljs-comment">// &#x6587;&#x4EF6; ./src/walk.ts</span>

<span class="hljs-comment">// &#x4E3A;&#x4FBF;&#x4E8E;&#x7406;&#x89E3;&#xFF0C;&#x6211;&#x5BF9;&#x4EE3;&#x7801;&#x8FDB;&#x884C;&#x4E86;&#x7CBE;&#x7B80;</span>
<span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> walk = (node: Node, ctx: Context): ChildNode | <span class="hljs-literal">null</span> | <span class="hljs-built_in">void</span> {
  <span class="hljs-keyword">const</span> <span class="hljs-keyword">type</span> = node.nodeType
  <span class="hljs-keyword">if</span> (<span class="hljs-keyword">type</span> == <span class="hljs-number">1</span>) {
    <span class="hljs-comment">// node&#x4E3A;Element&#x7C7B;&#x578B;</span>
    <span class="hljs-keyword">const</span> el = node as Element

    <span class="hljs-keyword">let</span> exp: <span class="hljs-built_in">string</span> | <span class="hljs-literal">null</span>

    <span class="hljs-keyword">if</span> ((exp = checkAttr(el, <span class="hljs-string">&apos;v-for&apos;</span>))) {
      <span class="hljs-keyword">return</span> _for(el, exp, ctx) <span class="hljs-comment">// &#x8FD4;&#x56DE;&#x6700;&#x8FD1;&#x4E00;&#x4E2A;&#x6CA1;&#x6709;`v-else-if`&#x6216;`v-else`&#x7684;&#x5144;&#x5F1F;&#x8282;&#x70B9;</span>
    }
  }
}
</code></pre>
<pre><code class="lang-ts"><span class="hljs-comment">// &#x6587;&#x4EF6; ./src/directives/for.ts</span>

<span class="hljs-comment">/* [\s\S]*&#x8868;&#x793A;&#x8BC6;&#x522B;&#x7A7A;&#x683C;&#x5B57;&#x7B26;&#x548C;&#x975E;&#x7A7A;&#x683C;&#x5B57;&#x7B26;&#x82E5;&#x5E72;&#x4E2A;&#xFF0C;&#x9ED8;&#x8BA4;&#x4E3A;&#x8D2A;&#x5A6A;&#x6A21;&#x5F0F;&#xFF0C;&#x5373; `(item, index) in value` &#x5C31;&#x4F1A;&#x5339;&#x914D;&#x6574;&#x4E2A;&#x5B57;&#x7B26;&#x4E32;&#x3002;
 * &#x4FEE;&#x6539;&#x4E3A;[\s\S]*?&#x5219;&#x4E3A;&#x61D2;&#x60F0;&#x6A21;&#x5F0F;&#xFF0C;&#x5373;`(item, index) in value`&#x53EA;&#x4F1A;&#x5339;&#x914D;`(item, index)`
 */</span>
<span class="hljs-keyword">const</span> forAliasRE = <span class="hljs-regexp">/([\s\S]*?)\s+(?:in)\s+([\s\S]*?)/</span>
<span class="hljs-comment">// &#x7528;&#x4E8E;&#x79FB;&#x9664;`(item, index)`&#x4E2D;&#x7684;`(`&#x548C;`)`</span>
<span class="hljs-keyword">const</span> stripParentRE= <span class="hljs-regexp">/^\(|\)$/g</span>
<span class="hljs-comment">// &#x7528;&#x4E8E;&#x5339;&#x914D;`item, index`&#x4E2D;&#x7684;`, index`&#xFF0C;&#x90A3;&#x4E48;&#x5C31;&#x53EF;&#x4EE5;&#x62BD;&#x53D6;&#x51FA;value&#x548C;index&#x6765;&#x72EC;&#x7ACB;&#x5904;&#x7406;</span>
<span class="hljs-keyword">const</span> forIteratorRE = <span class="hljs-regexp">/,([^,\}\]]*)(?:,([^,\}\]]*))?$/</span>

<span class="hljs-keyword">type</span> KeyToIndexMap = Map&lt;<span class="hljs-built_in">any</span>, <span class="hljs-built_in">number</span>&gt;

<span class="hljs-comment">// &#x4E3A;&#x4FBF;&#x4E8E;&#x7406;&#x89E3;&#xFF0C;&#x6211;&#x4EEC;&#x5047;&#x8BBE;&#x53EA;&#x63A5;&#x53D7;`v-for=&quot;val in values&quot;`&#x7684;&#x5F62;&#x5F0F;&#xFF0C;&#x5E76;&#x4E14;&#x6240;&#x6709;&#x5165;&#x53C2;&#x90FD;&#x662F;&#x6709;&#x6548;&#x7684;&#xFF0C;&#x5BF9;&#x5165;&#x53C2;&#x6709;&#x6548;&#x6027;&#x3001;&#x89E3;&#x6784;&#x7B49;&#x4EE3;&#x7801;&#x8FDB;&#x884C;&#x4E86;&#x5220;&#x51CF;</span>
<span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> _for = (el: Element, exp: <span class="hljs-built_in">string</span>, ctx: Context) =&gt; {
  <span class="hljs-comment">// &#x901A;&#x8FC7;&#x6B63;&#x5219;&#x8868;&#x8FBE;&#x5F0F;&#x62BD;&#x53D6;&#x8868;&#x8FBE;&#x5F0F;&#x5B57;&#x7B26;&#x4E32;&#x4E2D;`in`&#x4E24;&#x4FA7;&#x7684;&#x5B50;&#x8868;&#x8FBE;&#x5F0F;&#x5B57;&#x7B26;&#x4E32;</span>
  <span class="hljs-keyword">const</span> inMatch = exp.match(forAliasRE)

  <span class="hljs-comment">// &#x4FDD;&#x5B58;&#x4E0B;&#x4E00;&#x8F6E;&#x904D;&#x5386;&#x89E3;&#x6790;&#x7684;&#x6A21;&#x677F;&#x8282;&#x70B9;</span>
  <span class="hljs-keyword">const</span> nextNode = el.nextSibling

  <span class="hljs-comment">// &#x63D2;&#x5165;&#x951A;&#x70B9;&#xFF0C;&#x5E76;&#x5C06;&#x5E26;`v-for`&#x7684;&#x5143;&#x7D20;&#x4ECE;DOM&#x6811;&#x79FB;&#x9664;</span>
  <span class="hljs-keyword">const</span> parent = el.parentElement!
  <span class="hljs-keyword">const</span> anchor = <span class="hljs-keyword">new</span> Text(<span class="hljs-string">&apos;&apos;</span>)
  parent.insertBefore(anchor, el)
  parent.removeChild(el)

  <span class="hljs-keyword">const</span> sourceExp = inMatch[<span class="hljs-number">2</span>].trim() <span class="hljs-comment">// &#x83B7;&#x53D6;`(item, index) in value`&#x4E2D;`value`</span>
  <span class="hljs-keyword">let</span> valueExp = inMatch[<span class="hljs-number">1</span>].trim().replace(stripParentRE, <span class="hljs-string">&apos;&apos;</span>).trim() <span class="hljs-comment">// &#x83B7;&#x53D6;`(item, index) in value`&#x4E2D;`item, index`</span>
  <span class="hljs-keyword">let</span> indexExp: <span class="hljs-built_in">string</span> | <span class="hljs-literal">undefined</span>

  <span class="hljs-keyword">let</span> keyAttr = <span class="hljs-string">&apos;key&apos;</span>
  <span class="hljs-keyword">let</span> keyExp = 
    el.getAttribute(keyAttr) ||
    el.getAttribute(keyAttr = <span class="hljs-string">&apos;:key&apos;</span>) ||
    el.getAttribute(keyAttr = <span class="hljs-string">&apos;v-bind:key&apos;</span>)
  <span class="hljs-keyword">if</span> (keyExp) {
    el.removeAttribute(keyExp)
    <span class="hljs-comment">// &#x5C06;&#x8868;&#x8FBE;&#x5F0F;&#x5E8F;&#x5217;&#x5316;&#xFF0C;&#x5982;`value`&#x5E8F;&#x5217;&#x5316;&#x4E3A;`&quot;value&quot;`&#xFF0C;&#x8FD9;&#x6837;&#x5C31;&#x4E0D;&#x4F1A;&#x53C2;&#x4E0E;&#x540E;&#x9762;&#x7684;&#x8868;&#x8FBE;&#x5F0F;&#x8FD0;&#x7B97;</span>
    <span class="hljs-keyword">if</span> (keyAttr === <span class="hljs-string">&apos;key&apos;</span>) keyExp = <span class="hljs-built_in">JSON</span>.stringify(keyExp)
  }

  <span class="hljs-keyword">let</span> match
  <span class="hljs-keyword">if</span> (match = valueExp.match(forIteratorRE)) {
    valueExp = valueExp.replace(forIteratorRE, <span class="hljs-string">&apos;&apos;</span>).trim() <span class="hljs-comment">// &#x83B7;&#x53D6;`item, index`&#x4E2D;&#x7684;item</span>
    indexExp = match[<span class="hljs-number">1</span>].trim()  <span class="hljs-comment">// &#x83B7;&#x53D6;`item, index`&#x4E2D;&#x7684;index</span>
  }

  <span class="hljs-keyword">let</span> mounted = <span class="hljs-literal">false</span> <span class="hljs-comment">// false&#x8868;&#x793A;&#x9996;&#x6B21;&#x6E32;&#x67D3;&#xFF0C;true&#x8868;&#x793A;&#x66F4;&#x65B0;&#x6E32;&#x67D3;</span>
  <span class="hljs-keyword">let</span> blocks: Block[]
  <span class="hljs-keyword">let</span> childCtxs: Context[]
  <span class="hljs-keyword">let</span> keyToIndexMap: KeyToIndexMap <span class="hljs-comment">// &#x7528;&#x4E8E;&#x8BB0;&#x5F55;key&#x548C;&#x7D22;&#x5F15;&#x7684;&#x5173;&#x7CFB;&#xFF0C;&#x5F53;&#x53D1;&#x751F;&#x66F4;&#x65B0;&#x6E32;&#x67D3;&#x65F6;&#x5219;&#x590D;&#x7528;&#x5143;&#x7D20;</span>

  <span class="hljs-comment">// &#x5165;&#x53C2;source&#x4E3A;&#x6570;&#x7EC4;&#x65F6;&#xFF0C;&#x5219;&#x904D;&#x5386;&#x6570;&#x7EC4;&#x7684;&#x6BCF;&#x4E2A;&#x5143;&#x7D20;&#xFF0C;&#x5E76;&#x4EE5;&#x5143;&#x7D20;&#x4E3A;&#x57FA;&#x7840;&#x521B;&#x5EFA;&#x72EC;&#x7ACB;&#x7684;&#x4F5C;&#x7528;&#x57DF;</span>
  <span class="hljs-keyword">const</span> createChildContexts = (source: unknown): [Context[], KeyToIndexMap] =&gt; {
    <span class="hljs-keyword">const</span> map: KeyToIndexMap = <span class="hljs-keyword">new</span> Map()
    <span class="hljs-keyword">const</span> ctxs: Context[] = []

    <span class="hljs-keyword">if</span> (isArray(source)) {
      <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i &lt; source.length; i++) {
        ctxs.push(createChildContext(map, source[i], i))
      }
    }  

    <span class="hljs-keyword">return</span> [ctxs, map]
  }

  <span class="hljs-comment">// &#x4EE5;&#x96C6;&#x5408;&#x5143;&#x7D20;&#x4E3A;&#x57FA;&#x7840;&#x521B;&#x5EFA;&#x72EC;&#x7ACB;&#x7684;&#x4F5C;&#x7528;&#x57DF;</span>
  <span class="hljs-keyword">const</span> createChildContext = (
    map: KeyToIndexMap,
    value: <span class="hljs-built_in">any</span>, <span class="hljs-comment">// &#x5143;&#x7D20;</span>
    index: <span class="hljs-built_in">number</span> <span class="hljs-comment">// &#x5143;&#x7D20;&#x4F4D;&#x4E8E;&#x6570;&#x7EC4;&#x4E2D;&#x7684;&#x7D22;&#x5F15;&#x503C;</span>
  ): Context =&gt; {
    <span class="hljs-keyword">const</span> data: <span class="hljs-built_in">any</span> = {}
    data[valueExp] = value
    indexExp &amp;&amp; (data[indexExp] = index)
    <span class="hljs-comment">// &#x4E3A;&#x6BCF;&#x4E2A;&#x5B50;&#x5143;&#x7D20;&#x521B;&#x5EFA;&#x72EC;&#x7ACB;&#x7684;&#x4F5C;&#x7528;&#x57DF;</span>
    <span class="hljs-keyword">const</span> childCtx = createScopedContext(ctx, data)
    <span class="hljs-comment">// key&#x8868;&#x8FBE;&#x5F0F;&#x5728;&#x6240;&#x5728;&#x7684;&#x5B50;&#x5143;&#x7D20;&#x7684;&#x4F5C;&#x7528;&#x57DF;&#x4E0B;&#x8FD0;&#x7B97;</span>
    <span class="hljs-keyword">const</span> key = keyExp ? evaluate(childCtx.scope, keyExp) : index
    map.set(key, index)
    childCtx.key = key

    <span class="hljs-keyword">return</span> childCtx
  }

  <span class="hljs-comment">// &#x4E3A;&#x6BCF;&#x4E2A;&#x5B50;&#x5143;&#x7D20;&#x521B;&#x5EFA;&#x5757;&#x5BF9;&#x8C61;</span>
  <span class="hljs-keyword">const</span> mountBlock = (ctx: Conext, ref: Node) =&gt; {
    <span class="hljs-keyword">const</span> block = <span class="hljs-keyword">new</span> Block(el, ctx)
    block.key = ctx.key
    block.insert(parent, ref)
    <span class="hljs-keyword">return</span> block
  }

  ctx.effect(() =&gt; {
    <span class="hljs-keyword">const</span> source = evaluate(ctx.scope, sourceExp) <span class="hljs-comment">// &#x8FD0;&#x7B97;&#x51FA;`(item, index) in items`&#x4E2D;items&#x7684;&#x771F;&#x5B9E;&#x503C;</span>
    <span class="hljs-keyword">const</span> prevKeyToIndexMap = keyToIndexMap
    <span class="hljs-comment">// &#x751F;&#x6210;&#x65B0;&#x7684;&#x4F5C;&#x7528;&#x57DF;&#xFF0C;&#x5E76;&#x8BA1;&#x7B97;`key`,`:key`&#x6216;`v-bind:key`</span>
    ;[childCtxs, keyToIndexMap] = createChildContexts(source)
    <span class="hljs-keyword">if</span> (!mounted) {
      <span class="hljs-comment">// &#x4E3A;&#x6BCF;&#x4E2A;&#x5B50;&#x5143;&#x7D20;&#x521B;&#x5EFA;&#x5757;&#x5BF9;&#x8C61;&#xFF0C;&#x89E3;&#x6790;&#x5B50;&#x5143;&#x7D20;&#x7684;&#x5B50;&#x5B59;&#x5143;&#x7D20;&#x540E;&#x63D2;&#x5165;DOM&#x6811;</span>
      blocks = childCtxs.map(s =&gt; mountBlock(s, anchor))
      mounted = <span class="hljs-literal">true</span>
    }
    <span class="hljs-comment">// &#x7531;&#x4E8E;&#x6211;&#x4EEC;&#x793A;&#x4F8B;&#x53EA;&#x7814;&#x7A76;&#x9759;&#x6001;&#x89C6;&#x56FE;&#xFF0C;&#x56E0;&#x6B64;&#x91CD;&#x65B0;&#x6E32;&#x67D3;&#x7684;&#x4EE3;&#x7801;&#xFF0C;&#x6211;&#x4EEC;&#x540E;&#x9762;&#x518D;&#x6DF1;&#x5165;&#x4E86;&#x89E3;&#x5427;</span>
  })

  <span class="hljs-keyword">return</span> nextNode
}
</code></pre>
<p>&#x867D;&#x7136;&#x5DF2;&#x7ECF;&#x5C06;&#x4E0E;&#x9996;&#x6B21;&#x6E32;&#x67D3;&#x5173;&#x8054;&#x6027;&#x4E0D;&#x5F3A;&#x7684;&#x4EE3;&#x7801;&#x90FD;&#x5220;&#x9664;&#xFF0C;&#x4F46;&#x7531;&#x4E8E;&#x66F4;&#x65B0;&#x6E32;&#x67D3;&#x65F6;&#x4F1A;&#x5C06;&#x8FD0;&#x7B97;&#x7ED3;&#x679C;&#x548C;&#x4E0A;&#x4E00;&#x6B21;&#x6E32;&#x67D3;(&#x5F53;&#x7136;&#x5305;&#x542B;&#x9996;&#x6B21;&#x6E32;&#x67D3;)&#x7248;&#x672C;&#x8FDB;&#x884C;&#x5BF9;&#x6BD4;(diff)&#xFF0C;&#x6240;&#x4EE5;&#x65E0;&#x6CD5;&#x505A;&#x5230;&#x5B8C;&#x5168;&#x5E72;&#x51C0;&#x5730;&#x5448;&#x73B0;&#x4EC5;&#x4E0E;&#x9996;&#x6B21;&#x6E32;&#x67D3;&#x76F8;&#x5173;&#x7684;&#x4EE3;&#x7801;&#x3002;</p>
<h2 id="v-for&#x7684;&#x66F4;&#x65B0;&#x6E32;&#x67D3;"><a name="v-for&#x7684;&#x66F4;&#x65B0;&#x6E32;&#x67D3;" class="anchor-navigation-ex-anchor" href="#v-for&#x7684;&#x66F4;&#x65B0;&#x6E32;&#x67D3;"><i class="fa fa-link" aria-hidden="true"></i></a>1.2. v-for&#x7684;&#x66F4;&#x65B0;&#x6E32;&#x67D3;</h2>
<pre><code class="lang-ts"><span class="hljs-comment">// &#x6587;&#x4EF6; ./src/directives/for.ts</span>

<span class="hljs-comment">/* [\s\S]*&#x8868;&#x793A;&#x8BC6;&#x522B;&#x7A7A;&#x683C;&#x5B57;&#x7B26;&#x548C;&#x975E;&#x7A7A;&#x683C;&#x5B57;&#x7B26;&#x82E5;&#x5E72;&#x4E2A;&#xFF0C;&#x9ED8;&#x8BA4;&#x4E3A;&#x8D2A;&#x5A6A;&#x6A21;&#x5F0F;&#xFF0C;&#x5373; `(item, index) in value` &#x5C31;&#x4F1A;&#x5339;&#x914D;&#x6574;&#x4E2A;&#x5B57;&#x7B26;&#x4E32;&#x3002;
 * &#x4FEE;&#x6539;&#x4E3A;[\s\S]*?&#x5219;&#x4E3A;&#x61D2;&#x60F0;&#x6A21;&#x5F0F;&#xFF0C;&#x5373;`(item, index) in value`&#x53EA;&#x4F1A;&#x5339;&#x914D;`(item, index)`
 */</span>
<span class="hljs-keyword">const</span> forAliasRE = <span class="hljs-regexp">/([\s\S]*?)\s+(?:in)\s+([\s\S]*?)/</span>
<span class="hljs-comment">// &#x7528;&#x4E8E;&#x79FB;&#x9664;`(item, index)`&#x4E2D;&#x7684;`(`&#x548C;`)`</span>
<span class="hljs-keyword">const</span> stripParentRE= <span class="hljs-regexp">/^\(|\)$/g</span>
<span class="hljs-comment">// &#x7528;&#x4E8E;&#x5339;&#x914D;`item, index`&#x4E2D;&#x7684;`, index`&#xFF0C;&#x90A3;&#x4E48;&#x5C31;&#x53EF;&#x4EE5;&#x62BD;&#x53D6;&#x51FA;value&#x548C;index&#x6765;&#x72EC;&#x7ACB;&#x5904;&#x7406;</span>
<span class="hljs-keyword">const</span> forIteratorRE = <span class="hljs-regexp">/,([^,\}\]]*)(?:,([^,\}\]]*))?$/</span>

<span class="hljs-keyword">type</span> KeyToIndexMap = Map&lt;<span class="hljs-built_in">any</span>, <span class="hljs-built_in">number</span>&gt;

<span class="hljs-comment">// &#x4E3A;&#x4FBF;&#x4E8E;&#x7406;&#x89E3;&#xFF0C;&#x6211;&#x4EEC;&#x5047;&#x8BBE;&#x53EA;&#x63A5;&#x53D7;`v-for=&quot;val in values&quot;`&#x7684;&#x5F62;&#x5F0F;&#xFF0C;&#x5E76;&#x4E14;&#x6240;&#x6709;&#x5165;&#x53C2;&#x90FD;&#x662F;&#x6709;&#x6548;&#x7684;&#xFF0C;&#x5BF9;&#x5165;&#x53C2;&#x6709;&#x6548;&#x6027;&#x3001;&#x89E3;&#x6784;&#x7B49;&#x4EE3;&#x7801;&#x8FDB;&#x884C;&#x4E86;&#x5220;&#x51CF;</span>
<span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> _for = (el: Element, exp: <span class="hljs-built_in">string</span>, ctx: Context) =&gt; {
  <span class="hljs-comment">// &#x901A;&#x8FC7;&#x6B63;&#x5219;&#x8868;&#x8FBE;&#x5F0F;&#x62BD;&#x53D6;&#x8868;&#x8FBE;&#x5F0F;&#x5B57;&#x7B26;&#x4E32;&#x4E2D;`in`&#x4E24;&#x4FA7;&#x7684;&#x5B50;&#x8868;&#x8FBE;&#x5F0F;&#x5B57;&#x7B26;&#x4E32;</span>
  <span class="hljs-keyword">const</span> inMatch = exp.match(forAliasRE)

  <span class="hljs-comment">// &#x4FDD;&#x5B58;&#x4E0B;&#x4E00;&#x8F6E;&#x904D;&#x5386;&#x89E3;&#x6790;&#x7684;&#x6A21;&#x677F;&#x8282;&#x70B9;</span>
  <span class="hljs-keyword">const</span> nextNode = el.nextSibling

  <span class="hljs-comment">// &#x63D2;&#x5165;&#x951A;&#x70B9;&#xFF0C;&#x5E76;&#x5C06;&#x5E26;`v-for`&#x7684;&#x5143;&#x7D20;&#x4ECE;DOM&#x6811;&#x79FB;&#x9664;</span>
  <span class="hljs-keyword">const</span> parent = el.parentElement!
  <span class="hljs-keyword">const</span> anchor = <span class="hljs-keyword">new</span> Text(<span class="hljs-string">&apos;&apos;</span>)
  parent.insertBefore(anchor, el)
  parent.removeChild(el)

  <span class="hljs-keyword">const</span> sourceExp = inMatch[<span class="hljs-number">2</span>].trim() <span class="hljs-comment">// &#x83B7;&#x53D6;`(item, index) in value`&#x4E2D;`value`</span>
  <span class="hljs-keyword">let</span> valueExp = inMatch[<span class="hljs-number">1</span>].trim().replace(stripParentRE, <span class="hljs-string">&apos;&apos;</span>).trim() <span class="hljs-comment">// &#x83B7;&#x53D6;`(item, index) in value`&#x4E2D;`item, index`</span>
  <span class="hljs-keyword">let</span> indexExp: <span class="hljs-built_in">string</span> | <span class="hljs-literal">undefined</span>

  <span class="hljs-keyword">let</span> keyAttr = <span class="hljs-string">&apos;key&apos;</span>
  <span class="hljs-keyword">let</span> keyExp = 
    el.getAttribute(keyAttr) ||
    el.getAttribute(keyAttr = <span class="hljs-string">&apos;:key&apos;</span>) ||
    el.getAttribute(keyAttr = <span class="hljs-string">&apos;v-bind:key&apos;</span>)
  <span class="hljs-keyword">if</span> (keyExp) {
    el.removeAttribute(keyExp)
    <span class="hljs-comment">// &#x5C06;&#x8868;&#x8FBE;&#x5F0F;&#x5E8F;&#x5217;&#x5316;&#xFF0C;&#x5982;`value`&#x5E8F;&#x5217;&#x5316;&#x4E3A;`&quot;value&quot;`&#xFF0C;&#x8FD9;&#x6837;&#x5C31;&#x4E0D;&#x4F1A;&#x53C2;&#x4E0E;&#x540E;&#x9762;&#x7684;&#x8868;&#x8FBE;&#x5F0F;&#x8FD0;&#x7B97;</span>
    <span class="hljs-keyword">if</span> (keyAttr === <span class="hljs-string">&apos;key&apos;</span>) keyExp = <span class="hljs-built_in">JSON</span>.stringify(keyExp)
  }

  <span class="hljs-keyword">let</span> match
  <span class="hljs-keyword">if</span> (match = valueExp.match(forIteratorRE)) {
    valueExp = valueExp.replace(forIteratorRE, <span class="hljs-string">&apos;&apos;</span>).trim() <span class="hljs-comment">// &#x83B7;&#x53D6;`item, index`&#x4E2D;&#x7684;item</span>
    indexExp = match[<span class="hljs-number">1</span>].trim()  <span class="hljs-comment">// &#x83B7;&#x53D6;`item, index`&#x4E2D;&#x7684;index</span>
  }

  <span class="hljs-keyword">let</span> mounted = <span class="hljs-literal">false</span> <span class="hljs-comment">// false&#x8868;&#x793A;&#x9996;&#x6B21;&#x6E32;&#x67D3;&#xFF0C;true&#x8868;&#x793A;&#x91CD;&#x65B0;&#x6E32;&#x67D3;</span>
  <span class="hljs-keyword">let</span> blocks: Block[]
  <span class="hljs-keyword">let</span> childCtxs: Context[]
  <span class="hljs-keyword">let</span> keyToIndexMap: KeyToIndexMap <span class="hljs-comment">// &#x7528;&#x4E8E;&#x8BB0;&#x5F55;key&#x548C;&#x7D22;&#x5F15;&#x7684;&#x5173;&#x7CFB;&#xFF0C;&#x5F53;&#x53D1;&#x751F;&#x91CD;&#x65B0;&#x6E32;&#x67D3;&#x65F6;&#x5219;&#x590D;&#x7528;&#x5143;&#x7D20;</span>

  <span class="hljs-keyword">const</span> createChildContexts = (source: unknown): [Context[], KeyToIndexMap] =&gt; {
    <span class="hljs-keyword">const</span> map: KeyToIndexMap = <span class="hljs-keyword">new</span> Map()
    <span class="hljs-keyword">const</span> ctxs: Context[] = []

    <span class="hljs-keyword">if</span> (isArray(source)) {
      <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i &lt; source.length; i++) {
        ctxs.push(createChildContext(map, source[i], i))
      }
    }  

    <span class="hljs-keyword">return</span> [ctxs, map]
  }

  <span class="hljs-comment">// &#x4EE5;&#x96C6;&#x5408;&#x5143;&#x7D20;&#x4E3A;&#x57FA;&#x7840;&#x521B;&#x5EFA;&#x72EC;&#x7ACB;&#x7684;&#x4F5C;&#x7528;&#x57DF;</span>
  <span class="hljs-keyword">const</span> createChildContext = (
    map: KeyToIndexMap,
    value: <span class="hljs-built_in">any</span>, <span class="hljs-comment">// the item of collection</span>
    index: <span class="hljs-built_in">number</span> <span class="hljs-comment">// the index of item of collection</span>
  ): Context =&gt; {
    <span class="hljs-keyword">const</span> data: <span class="hljs-built_in">any</span> = {}
    data[valueExp] = value
    indexExp &amp;&amp; (data[indexExp] = index)
    <span class="hljs-comment">// &#x4E3A;&#x6BCF;&#x4E2A;&#x5B50;&#x5143;&#x7D20;&#x521B;&#x5EFA;&#x72EC;&#x7ACB;&#x7684;&#x4F5C;&#x7528;&#x57DF;</span>
    <span class="hljs-keyword">const</span> childCtx = createScopedContext(ctx, data)
    <span class="hljs-comment">// key&#x8868;&#x8FBE;&#x5F0F;&#x5728;&#x5BF9;&#x5E94;&#x5B50;&#x5143;&#x7D20;&#x7684;&#x4F5C;&#x7528;&#x57DF;&#x4E0B;&#x8FD0;&#x7B97;</span>
    <span class="hljs-keyword">const</span> key = keyExp ? evaluate(childCtx.scope, keyExp) : index
    map.set(key, index)
    childCtx.key = key

    <span class="hljs-keyword">return</span> childCtx
  }

  <span class="hljs-comment">// &#x4E3A;&#x6BCF;&#x4E2A;&#x5B50;&#x5143;&#x7D20;&#x521B;&#x5EFA;&#x5757;&#x5BF9;&#x8C61;</span>
  <span class="hljs-keyword">const</span> mountBlock = (ctx: Conext, ref: Node) =&gt; {
    <span class="hljs-keyword">const</span> block = <span class="hljs-keyword">new</span> Block(el, ctx)
    block.key = ctx.key
    block.insert(parent, ref)
    <span class="hljs-keyword">return</span> block
  }

  ctx.effect(() =&gt; {
    <span class="hljs-keyword">const</span> source = evaluate(ctx.scope, sourceExp) <span class="hljs-comment">// &#x8FD0;&#x7B97;&#x51FA;`(item, index) in items`&#x4E2D;items&#x7684;&#x771F;&#x5B9E;&#x503C;</span>
    <span class="hljs-keyword">const</span> prevKeyToIndexMap = keyToIndexMap
    <span class="hljs-comment">// &#x751F;&#x6210;&#x65B0;&#x7684;&#x4F5C;&#x7528;&#x57DF;&#xFF0C;&#x5E76;&#x8BA1;&#x7B97;`key`,`:key`&#x6216;`v-bind:key`</span>
    ;[childCtxs, keyToIndexMap] = createChildContexts(source)
    <span class="hljs-keyword">if</span> (!mounted) {
      <span class="hljs-comment">// &#x4E3A;&#x6BCF;&#x4E2A;&#x5B50;&#x5143;&#x7D20;&#x521B;&#x5EFA;&#x5757;&#x5BF9;&#x8C61;&#xFF0C;&#x89E3;&#x6790;&#x5B50;&#x5143;&#x7D20;&#x7684;&#x5B50;&#x5B59;&#x5143;&#x7D20;&#x540E;&#x63D2;&#x5165;DOM&#x6811;</span>
      blocks = childCtxs.map(s =&gt; mountBlock(s, anchor))
      mounted = <span class="hljs-literal">true</span>
    }
    <span class="hljs-keyword">else</span> {
      <span class="hljs-comment">// &#x66F4;&#x65B0;&#x6E32;&#x67D3;&#x903B;&#x8F91;&#xFF01;&#xFF01;</span>
      <span class="hljs-comment">// &#x6839;&#x636E;key&#x79FB;&#x9664;&#x66F4;&#x65B0;&#x540E;&#x4E0D;&#x5B58;&#x5728;&#x7684;&#x5143;&#x7D20;</span>
      <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i &lt; blocks.length; i++) {
        <span class="hljs-keyword">if</span> (!keyToIndexMap.has(blocks[i].key)) {
          blocks[i].remove()
        }
      }

      <span class="hljs-keyword">const</span> nextBlocks: Block[] = []
      <span class="hljs-keyword">let</span> i = childCtxs.length
      <span class="hljs-keyword">let</span> nextBlock: Block | <span class="hljs-literal">undefined</span>
      <span class="hljs-keyword">let</span> prevMovedBlock: Block | <span class="hljs-literal">undefined</span>
      <span class="hljs-keyword">while</span> (i--) {
        <span class="hljs-keyword">const</span> childCtx = childCtxs[i]
        <span class="hljs-keyword">const</span> oldIndex = prevKeyToIndexMap.get(childCtx.key)
        <span class="hljs-keyword">let</span> block
        <span class="hljs-keyword">if</span> (oldIndex == <span class="hljs-literal">null</span>) {
          <span class="hljs-comment">// &#x65E7;&#x89C6;&#x56FE;&#x4E2D;&#x6CA1;&#x6709;&#x8BE5;&#x5143;&#x7D20;&#xFF0C;&#x56E0;&#x6B64;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x65B0;&#x7684;&#x5757;&#x5BF9;&#x8C61;</span>
          block = mountBlock(childCtx, newBlock ? newBlock.el : anchor)
        }
        <span class="hljs-keyword">else</span> {
          <span class="hljs-comment">// &#x65E7;&#x89C6;&#x56FE;&#x4E2D;&#x6709;&#x8BE5;&#x5143;&#x7D20;&#xFF0C;&#x5143;&#x7D20;&#x590D;&#x7528;</span>
          block = blocks[oldIndex]
          <span class="hljs-comment">// &#x66F4;&#x65B0;&#x4F5C;&#x7528;&#x57DF;&#xFF0C;&#x7531;&#x4E8E;&#x5143;&#x7D20;&#x4E0B;&#x7684;`:value`,``&#x7B49;&#x90FD;&#x4F1A;&#x8DDF;&#x8E2A;scope&#x5BF9;&#x5E94;&#x5C5E;&#x6027;&#x7684;&#x53D8;&#x5316;&#xFF0C;&#x56E0;&#x6B64;&#x8FD9;&#x91CC;&#x53EA;&#x9700;&#x8981;&#x66F4;&#x65B0;&#x4F5C;&#x7528;&#x57DF;&#x4E0A;&#x7684;&#x5C5E;&#x6027;&#xFF0C;&#x5373;&#x53EF;&#x89E6;&#x53D1;&#x5B50;&#x5143;&#x7D20;&#x7684;&#x66F4;&#x65B0;&#x6E32;&#x67D3;</span>
          <span class="hljs-built_in">Object</span>.assign(block.ctx.scope, childCtx.scope)
          <span class="hljs-keyword">if</span> (oldIndex != i) {
            <span class="hljs-comment">// &#x5143;&#x7D20;&#x5728;&#x65B0;&#x65E7;&#x89C6;&#x56FE;&#x4E2D;&#x7684;&#x4F4D;&#x7F6E;&#x4E0D;&#x540C;&#xFF0C;&#x9700;&#x8981;&#x79FB;&#x52A8;</span>
            <span class="hljs-keyword">if</span> (
              blocks[oldIndex + <span class="hljs-number">1</span>] !== nextBlock ||
              prevMoveBlock === nextBlock
            ) {
              prevMovedBlock = block
              <span class="hljs-comment">// anchor&#x4F5C;&#x4E3A;&#x540C;&#x7EA7;&#x5B50;&#x5143;&#x7D20;&#x7684;&#x672B;&#x5C3E;</span>
              block.insert(parent, nextBlock ? nextBlock.el : anchor)
            }
          }
        }
        nextBlocks.unshift(nextBlock = block)
      }
      blocks = nextBlocks
    }
  })

  <span class="hljs-keyword">return</span> nextNode
}
</code></pre>
<h3 id="key&#x5143;&#x7D20;&#x590D;&#x7528;&#x7B97;&#x6CD5;&#x8BE6;&#x89E3;"><a name="key&#x5143;&#x7D20;&#x590D;&#x7528;&#x7B97;&#x6CD5;&#x8BE6;&#x89E3;" class="anchor-navigation-ex-anchor" href="#key&#x5143;&#x7D20;&#x590D;&#x7528;&#x7B97;&#x6CD5;&#x8BE6;&#x89E3;"><i class="fa fa-link" aria-hidden="true"></i></a>1.2.1. key&#x5143;&#x7D20;&#x590D;&#x7528;&#x7B97;&#x6CD5;&#x8BE6;&#x89E3;</h3>
<p>&#x4E0A;&#x8FF0;&#x4EE3;&#x7801;&#x6700;&#x96BE;&#x7406;&#x89E3;&#x5C31;&#x662F;&#x901A;&#x8FC7;<code>key</code>&#x590D;&#x7528;&#x5143;&#x7D20;&#x90A3;&#x4E00;&#x6BB5;&#x4E86;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">const</span> nextBlocks: Block[] = []
<span class="hljs-keyword">let</span> i = childCtxs.length
<span class="hljs-keyword">let</span> nextBlock: Block | <span class="hljs-literal">undefined</span>
<span class="hljs-keyword">let</span> prevMovedBlock: Block | <span class="hljs-literal">undefined</span>
<span class="hljs-keyword">while</span> (i--) {
  <span class="hljs-keyword">const</span> childCtx = childCtxs[i]
  <span class="hljs-keyword">const</span> oldIndex = prevKeyToIndexMap.get(childCtx.key)
  <span class="hljs-keyword">let</span> block
  <span class="hljs-keyword">if</span> (oldIndex == <span class="hljs-literal">null</span>) {
    <span class="hljs-comment">// &#x65E7;&#x89C6;&#x56FE;&#x4E2D;&#x6CA1;&#x6709;&#x8BE5;&#x5143;&#x7D20;&#xFF0C;&#x56E0;&#x6B64;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x65B0;&#x7684;&#x5757;&#x5BF9;&#x8C61;</span>
    block = mountBlock(childCtx, newBlock ? newBlock.el : anchor)
  }
  <span class="hljs-keyword">else</span> {
    <span class="hljs-comment">// &#x65E7;&#x89C6;&#x56FE;&#x4E2D;&#x6709;&#x8BE5;&#x5143;&#x7D20;&#xFF0C;&#x5143;&#x7D20;&#x590D;&#x7528;</span>
    block = blocks[oldIndex]
    <span class="hljs-comment">// &#x66F4;&#x65B0;&#x4F5C;&#x7528;&#x57DF;&#xFF0C;&#x7531;&#x4E8E;&#x5143;&#x7D20;&#x4E0B;&#x7684;`:value`,``&#x7B49;&#x90FD;&#x4F1A;&#x8DDF;&#x8E2A;scope&#x5BF9;&#x5E94;&#x5C5E;&#x6027;&#x7684;&#x53D8;&#x5316;&#xFF0C;&#x56E0;&#x6B64;&#x8FD9;&#x91CC;&#x53EA;&#x9700;&#x8981;&#x66F4;&#x65B0;&#x4F5C;&#x7528;&#x57DF;&#x4E0A;&#x7684;&#x5C5E;&#x6027;&#xFF0C;&#x5373;&#x53EF;&#x89E6;&#x53D1;&#x5B50;&#x5143;&#x7D20;&#x7684;&#x66F4;&#x65B0;&#x6E32;&#x67D3;</span>
    <span class="hljs-built_in">Object</span>.assign(block.ctx.scope, childCtx.scope)
    <span class="hljs-keyword">if</span> (oldIndex != i) {
      <span class="hljs-comment">// &#x5143;&#x7D20;&#x5728;&#x65B0;&#x65E7;&#x89C6;&#x56FE;&#x4E2D;&#x7684;&#x4F4D;&#x7F6E;&#x4E0D;&#x540C;&#xFF0C;&#x9700;&#x8981;&#x79FB;&#x52A8;</span>
      <span class="hljs-keyword">if</span> (
        <span class="hljs-comment">/* blocks[oldIndex + 1] !== nextBlock &#x7528;&#x4E8E;&#x5BF9;&#x91CD;&#x590D;&#x952E;&#x51CF;&#x5C11;&#x6CA1;&#x5FC5;&#x8981;&#x7684;&#x79FB;&#x52A8;&#xFF08;&#x5982;&#x65E7;&#x89C6;&#x56FE;&#x4E3A;1224&#xFF0C;&#x65B0;&#x89C6;&#x56FE;&#x4E3A;1242&#xFF09;
         * prevMoveBlock === nextBlock &#x7528;&#x4E8E;&#x5904;&#x7406;&#x5982;&#x65E7;&#x89C6;&#x56FE;&#x4E3A;123&#xFF0C;&#x65B0;&#x89C6;&#x56FE;&#x4E3A;312&#x65F6;&#xFF0C;blocks[oldIndex + 1] === nextBlock&#x5BFC;&#x81F4;&#x65E0;&#x6CD5;&#x6267;&#x884C;&#x5143;&#x7D20;&#x79FB;&#x52A8;&#x64CD;&#x4F5C;
         */</span>
        blocks[oldIndex + <span class="hljs-number">1</span>] !== nextBlock || 
        prevMoveBlock === nextBlock
      ) {
        prevMovedBlock = block
        <span class="hljs-comment">// anchor&#x4F5C;&#x4E3A;&#x540C;&#x7EA7;&#x5B50;&#x5143;&#x7D20;&#x7684;&#x672B;&#x5C3E;</span>
        block.insert(parent, nextBlock ? nextBlock.el : anchor)
      }
    }
  }
  nextBlocks.unshift(nextBlock = block)
}
</code></pre>
<p>&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x793A;&#x4F8B;&#x901A;&#x8FC7;&#x4EBA;&#x8089;&#x5355;&#x6B65;&#x8C03;&#x8BD5;&#x7406;&#x89E3;</p>
<h4 id="&#x793A;&#x4F8B;1"><a name="&#x793A;&#x4F8B;1" class="anchor-navigation-ex-anchor" href="#&#x793A;&#x4F8B;1"><i class="fa fa-link" aria-hidden="true"></i></a>&#x793A;&#x4F8B;1</h4>
<p>&#x65E7;&#x89C6;&#x56FE;(&#x5DF2;&#x6E32;&#x67D3;): 1,2,3
&#x65B0;&#x89C6;&#x56FE;(&#x5F85;&#x6E32;&#x67D3;): 3,2,1</p>
<ol>
<li><p>&#x5FAA;&#x73AF;&#x7B2C;&#x4E00;&#x8F6E;</p>
<pre><code class="lang-js"> childCtx.key = <span class="hljs-number">1</span>
 i = <span class="hljs-number">2</span>
 oldIndex = <span class="hljs-number">0</span>
 nextBlock = <span class="hljs-literal">null</span>
 prevMovedBlock = <span class="hljs-literal">null</span>
</code></pre>
<p> &#x5373;<code>prevMoveBlock === nextBlock</code>
 &#x4E8E;&#x662F;&#x5C06;&#x65E7;&#x89C6;&#x56FE;&#x7684;block&#x79FB;&#x52A8;&#x5230;&#x6700;&#x540E;&#xFF0C;&#x89C6;&#x56FE;(&#x5DF2;&#x6E32;&#x67D3;): 2,3,1</p>
</li>
<li><p>&#x5FAA;&#x73AF;&#x7B2C;&#x4E8C;&#x8F6E;</p>
<pre><code class="lang-js"> childCtx.key = <span class="hljs-number">2</span>
 i = <span class="hljs-number">1</span>
 oldIndex = <span class="hljs-number">1</span>
</code></pre>
<p> &#x66F4;&#x65B0;&#x4F5C;&#x7528;&#x57DF;</p>
</li>
<li><p>&#x5FAA;&#x73AF;&#x7B2C;&#x4E09;&#x8F6E;</p>
<pre><code class="lang-js"> childCtx.key = <span class="hljs-number">3</span>
 i = <span class="hljs-number">0</span>
 oldIndex = <span class="hljs-number">2</span>
 nextBlock = block(.key=<span class="hljs-number">2</span>)
 prevMovedBlock = block(.key=<span class="hljs-number">1</span>)
</code></pre>
<p> &#x4E8E;&#x662F;&#x5C06;&#x65E7;&#x89C6;&#x56FE;&#x7684;block&#x79FB;&#x52A8;&#x5230;nextBlock&#x524D;&#xFF0C;&#x89C6;&#x56FE;(&#x5DF2;&#x6E32;&#x67D3;): 3,2,1</p>
</li>
</ol>
<h4 id="&#x793A;&#x4F8B;2---&#x5B58;&#x5728;&#x91CD;&#x590D;&#x952E;"><a name="&#x793A;&#x4F8B;2---&#x5B58;&#x5728;&#x91CD;&#x590D;&#x952E;" class="anchor-navigation-ex-anchor" href="#&#x793A;&#x4F8B;2---&#x5B58;&#x5728;&#x91CD;&#x590D;&#x952E;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x793A;&#x4F8B;2 - &#x5B58;&#x5728;&#x91CD;&#x590D;&#x952E;</h4>
<p>&#x65E7;&#x89C6;&#x56FE;(&#x5DF2;&#x6E32;&#x67D3;): 1,2,2,4
&#x65B0;&#x89C6;&#x56FE;(&#x5F85;&#x6E32;&#x67D3;): 1,2,4,2</p>
<p>&#x6B64;&#x65F6;<code>prevKeyToIndexMap.get(2)</code>&#x8FD4;&#x56DE;<code>2</code>&#xFF0C;&#x800C;&#x4F4D;&#x4E8E;&#x7D22;&#x5F15;&#x4E3A;1&#x7684;2&#x7684;&#x4FE1;&#x606F;&#x88AB;&#x540E;&#x8005;&#x8986;&#x76D6;&#x4E86;&#x3002;</p>
<ol>
<li><p>&#x5FAA;&#x73AF;&#x7B2C;&#x4E00;&#x8F6E;</p>
<pre><code class="lang-js"> childCtx.key = <span class="hljs-number">2</span>
 i = <span class="hljs-number">3</span>
 oldIndex = <span class="hljs-number">2</span>
 nextBlock = <span class="hljs-literal">null</span>
 prevMovedBlock = <span class="hljs-literal">null</span>
</code></pre>
<p> &#x4E8E;&#x662F;&#x5C06;&#x65E7;&#x89C6;&#x56FE;&#x7684;block&#x79FB;&#x52A8;&#x5230;&#x6700;&#x540E;&#xFF0C;&#x89C6;&#x56FE;(&#x5DF2;&#x6E32;&#x67D3;): 1,2,4,2</p>
</li>
<li><p>&#x5FAA;&#x73AF;&#x7B2C;&#x4E8C;&#x8F6E;</p>
<pre><code class="lang-js"> childCtx.key = <span class="hljs-number">4</span>
 i = <span class="hljs-number">2</span>
 oldIndex = <span class="hljs-number">3</span>
 nextBlock = block(.key=<span class="hljs-number">2</span>)
 prevMovedBlock = block(.key=<span class="hljs-number">2</span>)
</code></pre>
<p> &#x4E8E;&#x662F;&#x5C06;&#x65E7;&#x89C6;&#x56FE;&#x7684;block&#x79FB;&#x52A8;&#x5230;nextBlock&#x524D;&#xFF0C;&#x89C6;&#x56FE;(&#x5DF2;&#x6E32;&#x67D3;): 1,2,4,2</p>
</li>
<li><p>&#x5FAA;&#x73AF;&#x7B2C;&#x4E09;&#x8F6E;</p>
<pre><code class="lang-js"> childCtx.key = <span class="hljs-number">2</span>
 i = <span class="hljs-number">1</span>
 oldIndex = <span class="hljs-number">2</span>
 nextBlock = block(.key=<span class="hljs-number">4</span>)
 prevMovedBlock = block(.key=<span class="hljs-number">4</span>)
</code></pre>
<p> &#x7531;&#x4E8E;<code>blocks[oldIndex+1] === nextBlock</code>&#xFF0C;&#x56E0;&#x6B64;&#x4E0D;&#x7528;&#x79FB;&#x52A8;&#x5143;&#x7D20;</p>
</li>
<li><p>&#x5FAA;&#x73AF;&#x7B2C;&#x56DB;&#x8F6E;</p>
<pre><code class="lang-js">childCtx.key = <span class="hljs-number">1</span>
i = <span class="hljs-number">0</span>
oldIndex = <span class="hljs-number">0</span>
</code></pre>
<p>&#x7531;&#x4E8E;<code>i === oldIndex</code>&#xFF0C;&#x56E0;&#x6B64;&#x4E0D;&#x7528;&#x79FB;&#x52A8;&#x5143;&#x7D20;</p>
</li>
</ol>
<h3 id="&#x548C;react&#x901A;&#x8FC7;key&#x590D;&#x7528;&#x5143;&#x7D20;&#x7684;&#x533A;&#x522B;&#xFF1F;"><a name="&#x548C;react&#x901A;&#x8FC7;key&#x590D;&#x7528;&#x5143;&#x7D20;&#x7684;&#x533A;&#x522B;&#xFF1F;" class="anchor-navigation-ex-anchor" href="#&#x548C;react&#x901A;&#x8FC7;key&#x590D;&#x7528;&#x5143;&#x7D20;&#x7684;&#x533A;&#x522B;&#xFF1F;"><i class="fa fa-link" aria-hidden="true"></i></a>1.2.2. &#x548C;React&#x901A;&#x8FC7;key&#x590D;&#x7528;&#x5143;&#x7D20;&#x7684;&#x533A;&#x522B;&#xFF1F;</h3>
<p><em>React&#x901A;&#x8FC7;<code>key</code>&#x590D;&#x7528;&#x5143;&#x7D20;&#x662F;&#x91C7;&#x53D6;&#x5982;&#x4E0B;&#x7B97;&#x6CD5;</em></p>
<ol>
<li>&#x7B2C;&#x4E00;&#x6B21;&#x904D;&#x5386;&#x65B0;&#x65E7;&#x5143;&#x7D20;(<em>&#x5DE6;&#x5230;&#x53F3;</em>)<ol>
<li>&#x82E5;key&#x4E0D;&#x540C;&#x5373;&#x8DF3;&#x51FA;&#x904D;&#x5386;&#xFF0C;&#x8FDB;&#x5165;&#x7B2C;&#x4E8C;&#x8F6E;&#x904D;&#x5386;<ul>
<li>&#x6B64;&#x65F6;&#x901A;&#x8FC7;&#x53D8;&#x91CF;<code>lastPlacedIndex</code>&#x8BB0;&#x5F55;&#x6700;&#x540E;&#x4E00;&#x4E2A;<code>key</code>&#x5339;&#x914D;&#x7684;&#x65E7;&#x5143;&#x7D20;&#x4F4D;&#x7F6E;&#x7528;&#x4E8E;&#x63A7;&#x5236;&#x65E7;&#x5143;&#x7D20;&#x79FB;&#x52A8;</li>
</ul>
</li>
<li>&#x82E5;key&#x76F8;&#x540C;&#x4F46;&#x5143;&#x7D20;&#x7C7B;&#x578B;&#x4E0D;&#x540C;&#xFF0C;&#x5219;&#x521B;&#x5EFA;&#x65B0;&#x5143;&#x7D20;&#x66FF;&#x6362;&#x6389;&#x65E7;&#x5143;&#x7D20;</li>
</ol>
</li>
<li>&#x904D;&#x5386;&#x5269;&#x4E0B;&#x672A;&#x904D;&#x5386;&#x7684;&#x65E7;&#x5143;&#x7D20; - &#x4EE5;<code>&#x65E7;&#x5143;&#x7D20;.key</code>&#x4E3A;&#x952E;&#xFF0C;<code>&#x65E7;&#x5143;&#x7D20;</code>&#x4E3A;&#x503C;&#x901A;&#x8FC7;Map&#x5B58;&#x50A8;</li>
<li>&#x7B2C;&#x4E8C;&#x6B21;&#x904D;&#x5386;&#x5269;&#x4E0B;&#x672A;&#x904D;&#x5386;&#x7684;&#x65B0;&#x5143;&#x7D20;(<em>&#x5DE6;&#x5230;&#x53F3;</em>)<ol>
<li>&#x4ECE;Map&#x67E5;&#x627E;&#x662F;&#x5426;&#x5B58;&#x5728;&#x7684;&#x65E7;&#x5143;&#x7D20;&#xFF0C;&#x82E5;&#x6CA1;&#x6709;&#x5219;&#x521B;&#x5EFA;&#x65B0;&#x5143;&#x7D20;</li>
<li>&#x82E5;&#x5B58;&#x5728;&#x5219;&#x6309;&#x5982;&#x4E0B;&#x89C4;&#x5219;&#x64CD;&#x4F5C;&#xFF1A;<ul>
<li>&#x82E5;&#x4ECE;Map&#x67E5;&#x627E;&#x7684;&#x65E7;&#x5143;&#x7D20;&#x7684;&#x4F4D;&#x7F6E;&#x5927;&#x4E8E;<code>lastPlacedIndex</code>&#x5219;&#x5C06;&#x65E7;&#x5143;&#x7D20;&#x7684;&#x4F4D;&#x7F6E;&#x8D4B;&#x503C;&#x7ED9;<code>lastPlacedIndex</code>&#xFF0C;&#x82E5;&#x5143;&#x7D20;&#x7C7B;&#x578B;&#x76F8;&#x540C;&#x5219;&#x590D;&#x7528;&#x65E7;&#x5143;&#x7D20;&#xFF0C;&#x5426;&#x5219;&#x521B;&#x5EFA;&#x65B0;&#x5143;&#x7D20;&#x66FF;&#x6362;&#x6389;&#x65E7;&#x5143;&#x7D20;</li>
<li>&#x82E5;&#x4ECE;Map&#x67E5;&#x627E;&#x7684;&#x65E7;&#x5143;&#x7D20;&#x7684;&#x4F4D;&#x7F6E;&#x5C0F;&#x4E8E;<code>lastPlacedIndex</code>&#x5219;&#x8868;&#x793A;&#x65E7;&#x5143;&#x7D20;&#x5411;&#x53F3;&#x79FB;&#x52A8;&#xFF0C;&#x82E5;&#x5143;&#x7D20;&#x7C7B;&#x578B;&#x76F8;&#x540C;&#x5219;&#x590D;&#x7528;&#x65E7;&#x5143;&#x7D20;&#xFF0C;&#x5426;&#x5219;&#x521B;&#x5EFA;&#x65B0;&#x5143;&#x7D20;&#x66FF;&#x6362;&#x6389;&#x65E7;&#x5143;&#x7D20;(<code>lastPlacedIndex</code>&#x7684;&#x503C;&#x4FDD;&#x6301;&#x4E0D;&#x53D8;)</li>
</ul>
</li>
</ol>
</li>
<li>&#x6700;&#x540E;&#x5269;&#x4E0B;&#x672A;&#x904D;&#x5386;&#x7684;&#x65E7;&#x5143;&#x7D20;&#x5C06;&#x88AB;&#x5220;&#x9664;</li>
</ol>
<p>&#x7B2C;&#x4E8C;&#x6B21;&#x904D;&#x5386;&#x65F6;&#x79FB;&#x52A8;&#x5224;&#x65AD;&#x662F;&#xFF0C;&#x5047;&#x5B9A;<code>lastPlacedIndex</code>&#x5DE6;&#x4FA7;&#x7684;&#x65E7;&#x5143;&#x7D20;&#x5DF2;&#x7ECF;&#x548C;&#x65B0;&#x5143;&#x7D20;&#x5339;&#x914D;&#x4E14;&#x5DF2;&#x6392;&#x5E8F;&#xFF0C;&#x82E5;&#x53D1;&#x73B0;&#x65E7;&#x5143;&#x7D20;&#x7684;&#x4F4D;&#x7F6E;&#x5C0F;&#x4E8E;<code>lastPlacedIndex</code>&#xFF0C;&#x5219;&#x8868;&#x793A;<code>lastPlacedIndex</code>&#x5DE6;&#x4FA7;&#x6709;&#x5F02;&#x7C7B;&#x5FC5;&#x987B;&#x5411;&#x53F3;&#x632A;&#x52A8;&#x3002;</p>
<p>&#x800C;<em>petite-vue&#x7684;&#x7B97;&#x6CD5;&#x662F;</em></p>
<ol>
<li>&#x6BCF;&#x6B21;&#x6E32;&#x67D3;&#x65F6;&#x90FD;&#x4F1A;&#x751F;&#x6210;&#x4EE5;<code>&#x5143;&#x7D20;.key</code>&#x4E3A;&#x952E;&#xFF0C;<code>&#x5143;&#x7D20;</code>&#x4E3A;&#x503C;&#x901A;&#x8FC7;Map&#x5B58;&#x50A8;&#xFF0C;&#x5E76;&#x901A;&#x8FC7;<code>prevKeyToIndexMap</code>&#x4FDD;&#x7559;&#x6307;&#x5411;&#x4E0A;&#x4E00;&#x6B21;&#x6E32;&#x67D3;&#x7684;Map</li>
<li>&#x904D;&#x5386;&#x65E7;&#x5143;&#x7D20;&#xFF0C;&#x901A;&#x8FC7;&#x5F53;&#x524D;Map&#x7B5B;&#x9009;&#x51FA;&#x5F53;&#x524D;&#x6E32;&#x67D3;&#x4E2D;&#x5C06;&#x88AB;&#x79FB;&#x9664;&#x7684;&#x5143;&#x7D20;&#xFF0C;&#x5E76;&#x6CE8;&#x610F;&#x79FB;&#x9664;</li>
<li>&#x904D;&#x5386;&#x65B0;&#x5143;&#x7D20;(<em>&#x53F3;&#x5230;&#x5DE6;</em>)<ol>
<li>&#x82E5;key&#x76F8;&#x540C;&#x5219;&#x590D;&#x7528;</li>
<li>&#x82E5;key&#x4E0D;&#x540C;&#x5219;&#x901A;&#x8FC7;&#x65E7;Map&#x5BFB;&#x627E;&#x65E7;&#x5143;&#x7D20;&#xFF0C;&#x5E76;&#x63D2;&#x5165;&#x6700;&#x53F3;&#x6700;&#x8FD1;&#x4E00;&#x4E2A;&#x5DF2;&#x5904;&#x7406;&#x7684;&#x5143;&#x7D20;&#x524D;&#x9762;</li>
</ol>
</li>
</ol>
<p>&#x5B83;&#x4EEC;&#x7684;<em>&#x5DEE;&#x522B;</em></p>
<ol>
<li><p>petite-vue&#x65E0;&#x6CD5;&#x5904;&#x7406;key&#x76F8;&#x540C;&#x4F46;&#x5143;&#x7D20;&#x7C7B;&#x578B;&#x4E0D;&#x540C;&#x7684;&#x60C5;&#x51B5;&#xFF08;&#x5E94;&#x8BE5;&#x8BF4;&#x4E0D;&#x7528;&#x5904;&#x7406;&#x6BD4;&#x8F83;&#x9002;&#x5408;&#xFF09;&#xFF0C;&#x800C;React&#x53EF;&#x4EE5;</p>
<pre><code class="lang-js"><span class="hljs-comment">// petite-vue</span>
createApp({
  App: {
    <span class="hljs-comment">// &#x6839;&#x672C;&#x6CA1;&#x6709;&#x53EF;&#x80FD;key&#x76F8;&#x540C;&#x800C;&#x5143;&#x7D20;&#x7C7B;&#x578B;&#x4E0D;&#x540C;&#x561B;</span>
    $template: <span class="hljs-string">`
    &lt;div v-for=&quot;item in items&quot; :key=&quot;item.id&quot;&gt;&lt;/div&gt;
    `</span>
  }
})

<span class="hljs-comment">// React</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">App</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">const</span> items = [...]
  <span class="hljs-keyword">return</span> (
    items.map(item =&gt; {
      <span class="hljs-keyword">if</span> (item.type === <span class="hljs-string">&apos;span&apos;</span>) {
        <span class="hljs-keyword">return</span> (<span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">span</span> <span class="hljs-attr">key</span>=<span class="hljs-string">{item.id}</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span></span>)
      }
      <span class="hljs-keyword">else</span> {
        <span class="hljs-keyword">return</span> (<span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">key</span>=<span class="hljs-string">{item.id}</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>)
      }
    })
  )
}
</code></pre>
</li>
<li><p>&#x7531;&#x4E8E;petite-vue&#x5BF9;&#x91CD;&#x590D;key&#x8FDB;&#x884C;&#x4F18;&#x5316;&#xFF0C;&#x800C;React&#x4F1A;&#x5BF9;&#x91CD;&#x590D;key&#x6267;&#x884C;&#x540C;&#x6837;&#x7684;&#x5224;&#x65AD;&#x548C;&#x64CD;&#x4F5C;</p>
</li>
<li>petite-vue&#x662F;&#x5373;&#x65F6;&#x79FB;&#x52A8;&#x5143;&#x7D20;&#xFF0C;&#x800C;React&#x662F;&#x8FD0;&#x7B97;&#x540E;&#x518D;&#x79FB;&#x52A8;&#x5143;&#x7D20;&#xFF0C;&#x5E76;&#x4E14;&#x5BF9;&#x4E8E;&#x65E7;&#x89C6;&#x56FE;&#x4E3A;<code>123</code>&#xFF0C;&#x65B0;&#x89C6;&#x56FE;&#x4E3A;<code>312</code>&#x800C;&#x8A00;&#xFF0C;petite-vue&#x5C06;&#x79FB;&#x52A8;3&#x6B21;&#x5143;&#x7D20;&#xFF0C;&#x800C;React&#x4EC5;&#x79FB;&#x52A8;2&#x6B21;&#x5143;&#x7D20;</li>
</ol>
<h2 id="&#x5C0F;&#x7ED3;"><a name="&#x5C0F;&#x7ED3;" class="anchor-navigation-ex-anchor" href="#&#x5C0F;&#x7ED3;"><i class="fa fa-link" aria-hidden="true"></i></a>1.3. &#x5C0F;&#x7ED3;</h2>
<p>&#x5F71;&#x54CD;DOM&#x6811;&#x7ED3;&#x6784;&#x7684;<code>v-if</code>&#x548C;<code>v-for</code>&#x7684;&#x8BB2;&#x89E3;&#x5230;&#x6B64;&#x4E3A;&#x6B62;&#xFF0C;&#x540E;&#x9762;&#x6211;&#x4EEC;&#x770B;&#x770B;<code>v-bind</code>&#x7B49;&#x6307;&#x4EE4;&#x662F;&#x5982;&#x4F55;&#x5B9E;&#x73B0;&#x7684;&#x5427;&#xFF01;</p>
<script type="text/javascript">
  /**
   * Donation
   */
  function showQR(){
    var rewardButton = document.getElementById('rewardButton')
    var qr = document.getElementById('QR')
    if (rewardButton) {
      rewardButton.style.display = 'none'
    }
    if (qr) {
      qr.style.display = 'block'
    }
  }
  setInterval(showQR, 1000)

  /**
   * Baidu analysis
   */
  var _hmt = _hmt || [];
  (function() {
    var hm = document.createElement("script");
    hm.src = "https://hm.baidu.com/hm.js?8bca1e3aa362a7f85dbe8304cff65225";
    var s = document.getElementsByTagName("script")[0]; 
    s.parentNode.insertBefore(hm, s);
  })();
</script>


<footer class="page-footer"><span class="copyright">Copyright &#xA9; fsjohnhuang 2022 all right reserved&#xFF0C;powered by Gitbook</span><span class="footer-modification">File Modify: 
2022-04-26 09:46:33
</span></footer>
                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="2_initial_rendering_and_rerendering_for_v-if.html" class="navigation navigation-prev " aria-label="Previous page: 条件v-if的首次渲染和重新渲染">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="4_directives/1_attribute_binding.html" class="navigation navigation-next " aria-label="Next page: 属性绑定v-bind的工作原理">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"循环v-for的首次渲染和重新渲染","level":"2.4","depth":1,"next":{"title":"属性绑定v-bind的工作原理","level":"2.5","depth":1,"path":"1_online_rendering/4_directives/1_attribute_binding.md","ref":"1_online_rendering/4_directives/1_attribute_binding.md","articles":[]},"previous":{"title":"条件v-if的首次渲染和重新渲染","level":"2.3","depth":1,"path":"1_online_rendering/2_initial_rendering_and_rerendering_for_v-if.md","ref":"1_online_rendering/2_initial_rendering_and_rerendering_for_v-if.md","articles":[]},"dir":"ltr"},"config":{"plugins":["-lunr","-search","-sharing","theme-comscore","search-pro","code","back-to-top-button","tbfed-pagefooter","chapter-fold","expandable-chapters","splitter","anchor-navigation-ex","donate","sharing-plus","baidu"],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"tbfed-pagefooter":{"copyright":"Copyright &copy fsjohnhuang 2022"},"chapter-fold":{},"splitter":{},"search-pro":{},"sharing-plus":{"qq":false,"all":["facebook","google","twitter","instapaper","linkedin","pocket","stumbleupon"],"douban":false,"facebook":true,"weibo":false,"instapaper":false,"whatsapp":false,"hatenaBookmark":false,"twitter":true,"messenger":false,"line":false,"vk":false,"pocket":true,"google":false,"viber":false,"stumbleupon":false,"qzone":false,"linkedin":false},"code":{"copyButtons":true},"donate":{"alipay":"","alipayText":"支付宝打赏","button":"赞赏","title":"","wechat":"/petite-vue-source-reading/images/wechat.jpg","wechatText":"微信打赏"},"fontsettings":{"theme":"white","family":"sans","size":2},"highlight":{},"anchor-navigation-ex":{"associatedWithSummary":true,"float":{"floatIcon":"fa fa-navicon","level1Icon":"","level2Icon":"","level3Icon":"","showLevelIcon":false},"mode":"float","multipleH1":true,"pageTop":{"level1Icon":"","level2Icon":"","level3Icon":"","showLevelIcon":false},"printLog":false,"showGoTop":false,"showLevel":true},"theme-comscore":{},"back-to-top-button":{},"baidu":{"token":"8bca1e3aa362a7f85dbe8304cff65225"},"sharing":{"qq":false,"all":["qq","qzone","weibo","whatsapp","twitter","facebook","line"],"douban":false,"facebook":false,"weibo":false,"instapaper":false,"whatsapp":false,"hatenaBookmark":false,"twitter":false,"messenger":false,"line":false,"vk":false,"pocket":false,"google":false,"viber":false,"stumbleupon":false,"qzone":false,"linkedin":false},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false},"expandable-chapters":{}},"theme":"default","author":"^_^肥仔John","lang":"zh-cn","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"isbn":"","variables":{},"title":"petite-vue源码剖析","gitbook":"*","description":""},"file":{"path":"1_online_rendering/3_initial_rendering_and_rerendering_for_v-for.md","mtime":"2022-04-26T01:46:33.500Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2022-05-05T01:20:11.998Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-search-pro/jquery.mark.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search-pro/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-code/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-back-to-top-button/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-chapter-fold/chapter-fold.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-expandable-chapters/expandable-chapters.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-splitter/splitter.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-donate/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sharing-plus/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-baidu/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-theme-comscore/test.js"></script>
        
    

    </body>
</html>

